package edu.zut2.poly_demo;

/**
 * 向下转型:强制转换
 */
public class Test2 {

    public static void main(String[] args) {

        Test2 test2 = new Test2();
        Bird bird = test2.getBird();
        /**
         * 向下转型   父类类型的对象转换成子类类型的引用变量
         */
        if (bird instanceof Pigeon) {
            Pigeon pigeon = (Pigeon) bird;
            pigeon.fly();
        }

        /**
         * 类型检查，一般向下转型需要提前做一个类型判断，避免出现类型转换异常
         * obj instanceof 类型
         */
        if (bird instanceof Ostrich){
            Ostrich ostrich = (Ostrich) bird;
            ostrich.fly();
        }
    }

    public Bird getBird(){
        Bird bird = new Pigeon("鸽子一号","白色","送信");
        return bird;
    }

}

